
<?php
	
	if(isset($_POST['call_user'])):
		$to = $_POST['to_number'];
		$user_email = $_POST['to_email'];
		$err="";
		if ($to=="") {
			$err = "Must specify user phone number";
		}
		elseif($user_email==""){
			$err = "Must specify user email";
		}
		else{
			require "Services/Twilio.php";
			
			/* Set our AccountSid and AuthToken */
			$AccountSid = "AC015617a1e01b4cf09297fc31ca4846ae";
			$AuthToken = "6ba25acfcfea9ea96c5cb820411cf159";
			
			//$AccountSid = 'AP0f6252cd23094771a4ffb23474f8f1ba';
			
			/* Your Twilio Number or an Outgoing Caller ID you have previously validated
				with Twilio */
			//$from = '+1 424-204-5117';
			$from= '+1 415-599-2671';
			
			/* Number you wish to call */
			//$to= '+1 855-778-8648';
			
			
			/* Directory location for callback.php file (for use in REST URL)*/
			$url = 'http://qlabz.com/miqdad/twilio/';
			
			/* Instantiate a new Twilio Rest Client */
			$client = new Services_Twilio($AccountSid, $AuthToken);
			$err= '';
			try{
				$client->account->sms_messages->create($from, $to, "Just test");
				$err = 'Message sent <br />';
			}catch (Exception $e) {
				$err = 'Error sending message: ' . $e->getMessage() . "<br />";
				
			}
			
			/* make Twilio REST request to initiate outgoing call */
			try {
				
				
				
				$options = array("StatusCallback" => $url."end_of_call.php?email=$user_email");
				$call = $client->account->calls->create($from, $to, $url . 'callback.php',$options);
				$err.="Call done ....";
				unset($_POST);
			}catch (Exception $e) {
				$err .= 'Error starting phone call: ' . $e->getMessage() . "<br />";
				require_once ('end_of_call.php');
				$err .= sendMail($user_email);
			}
			
		}
		echo $err;
	endif;

?>
<form name="justActivate" action="start.php" method="post">
	<table>
		<tr>
			<td>USER PHONE NUMBER</td>
			<td><input type="text" name="to_number" value="<?php echo $_POST['to_number']; ?>" /></td>
		</tr>
		<tr>
			<td>USER EMAIL</td>
			<td><input type="text" name="to_email" value="<?php echo $_POST['to_email']; ?>" /></td>
		</tr>
		<tr>
			<td align="right"><input type="submit" name="call_user" value="Send Activation" /></td>
		</tr>
	</table>
</form>